<template>
  <page>
    <template v-slot:header>
      <h2>
        <i class="icon iconfont">&#xe612;</i>
        <span>{{ $t("patch.cve_group") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <p class="kylin-page-label">{{ $t("patch.cve_group_description") }}</p>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
      >
        <add-form
          v-if="type === 'create'"
          :row_id="id"
          @click="handleClose"
        ></add-form>
        <!-- <delete
          v-else-if="type === 'delete'"
          :content="delete_content"
          :handleCancel="handleClose"
          :handleOk="handleDeleteOk"
        >
        </delete> -->
      </el-dialog>
      <ky-table
        :search_data="search_data"
        :getData="getCveGroupList"
        :showSelect="false"
        :search_criteria="search_criteria"
        ref="table"
      >
        <template v-slot:table-action>
          <authorization-button class="kylin-item-button" @click="handleAdd"
            >{{ $t("configure.add") }}
          </authorization-button>
          <!-- <authorization-button
            class="kylin-item-button"
            @click="handleDelete"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
            >{{ $t("delete") }}
          </authorization-button> -->
        </template>
        <template v-slot:table>
          <el-table-column prop="groupName" :label="$t('patch.cve_group_name')">
          </el-table-column>
          <el-table-column label="CVE">
            <template slot-scope="scope">
              <router-link :to="$route.path + scope.row.id">
                {{ scope.row.cveNum }}
              </router-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="departmentName"
            :label="$t('configure.ascription')"
          >
          </el-table-column>
          <el-table-column prop="createdUser" :label="$t('configure.creator')">
          </el-table-column>
          <el-table-column prop="createdAt" :label="$t('create_time')">
          </el-table-column>
          <el-table-column prop="updatedUser" :label="$t('configure.reviser')">
          </el-table-column>
          <el-table-column
            prop="updatedAt"
            :label="$t('configure.last_revision_time')"
          >
          </el-table-column>
          <el-table-column prop="description" :label="$t('description')">
          </el-table-column>
          <el-table-column :label="$t('action')">
            <template slot-scope="scope">
              <el-popconfirm
                :title="$t('patch.cve_group_delete_confirm')"
                @confirm="handleDelete(scope.row)"
                icon-color="red"
                cancel-button-type="default"
                confirm-button-type="danger"
              >
                <authorization-button
                  type="text"
                  slot="reference"
                  size="medium"
                  class="kylin-delete"
                >
                  {{ $t("delete") }}
                </authorization-button>
              </el-popconfirm>
              <authorization-button
                @click.native.prevent="handleEdit(scope.row)"
                type="text"
                size="medium"
              >
                {{ $t("edit") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleIssue(scope.row)"
                type="text"
                size="medium"
              >
                {{ $t("patch.one_click_repair") }}
              </authorization-button>
            </template>
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import { getCveGroupList, deleteCveGroup, issueByGroup } from "@/api/cve_group";
import AuthorizationButton from "@/components/AuthorizationButton";
import AddForm from "./form/AddForm.vue";
import Delete from "@/components/Delete";
import { getDepartments } from "@/api/user";
export default {
  name: "cve_group",
  components: {
    Page,
    kyTable,
    AuthorizationButton,
    AddForm,
    Delete,
  },
  activated() {
      this.$refs.table.handleSearch();
  },
  data() {
    return {
      display: false,
      showSelect: false,
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
        userId: this.$store.getters.getUserId,
      },
      id: "",
      title: "",
      type: "",
      delete_content: "",
      search_data: {
        groupName: {
          name: "patch.cve_group_name",
          type: "text",
        },
        departmentId: {
          name: "configure.ascription",
          type: "select",
          items: [],
          func: this.getDepartment,
        },
        createdUser: {
          name: "configure.creator",
          type: "text",
        },
        createdAt: {
          name: "create_time",
          type: "date",
        },
        updatedUser: {
          name: "configure.reviser",
          type: "text",
        },
        updatedAt: {
          name: "configure.last_revision_time",
          type: "date",
        },
        description: {
          name: "description",
          type: "text",
        },
      },
    };
  },
  methods: {
    getCveGroupList,
    handleClose(type) {
      this.display = false;
      this.title = "";
      this.type = "";
      this.id = "";
      if (type === "success") {
        this.$refs.table.refresh();
      }
    },
    getDepartment() {
      getDepartments().then((res) => {
        if (res.data.code === "0") {
          let data = res.data.data;
          data.forEach((item) => {
            this.search_data.departmentId.items.push({
              key: item.name,
              name: item.name,
            });
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      });
    },
    handleIssue(row) {
      issueByGroup(row.id).then((res) => {
        this.$router.push({
          name: "log_detail",
          params: { id: res.data.data.id },
        });
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
        this.$refs.table.refresh();
      });
    },
    handleAdd() {
      this.display = true;
      this.title = this.$t("configure.add");
      this.type = "create";
    },
    handleEdit(row) {
      this.id = row.id;
      this.display = true;
      this.title = this.$t("edit");
      this.type = "create";
    },
    handleDelete(row) {
      /* this.display = true;
      this.type = "delete";
      this.title = this.$t("patch.delete_cve_group");
      let arr = [];
      let str = "";
      if (this.$refs.table.select_id == "2") {
        str = this.$refs.table.total.toString();
      } else {
        this.$refs.table.selectRow.rows.map(function (row) {
          arr.push(row.name);
        });
        str = arr.toString();
      }
      this.delete_content = this.$t("patch.delete_cve_group_content").replace(
        "%s",
        str
      ); */
      deleteCveGroup(row.id).then((res) => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
        this.$refs.table.refresh();
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.size {
  width: 134px;
}
.label {
  font-weight: 600;
  margin-right: 10px;
}
</style>